package com.chuangjiangx.member.basic.web.websocket;

import com.chuangjiangx.commons.JacksonUtils;
import com.chuangjiangx.member.score.ddd.domain.event.ClaimCodeExchangeEvent;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenhao
 * 会员取货码核销成功
  */
@Slf4j
public class MbrClaimCodeUseListener {
    @Autowired
    private ObjectMapper objectMapper;

    public void handleMessage(Serializable message) {
        if(null == message){return;}
        // 会员取货码核销成功
        String json = message.toString();
        log.info("会员取货码核销成功回调事件：{}", json);
        if(StringUtils.isBlank(json)){
            return;
        }
        ClaimCodeExchangeEvent exchangeEvent = JacksonUtils.toObject(objectMapper, json, ClaimCodeExchangeEvent.class);
        Map<String, Object> map = new HashMap<>(8);
        /**
         * msgType
         * 1 - 付款码付款消息
         * 2 - 会员卡券核销成功消息
         * 3 - 提货码核销成功消息
         */
        map.put("msgType", "3");
        map.put("memberId",exchangeEvent.getMemberId());
        map.put("code", exchangeEvent.getClaimCode());
        try {
            MemberCodePayWebSocketHandler.sendMessageToCustomer(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
